Vue.component('login',{
    data(){
        return{
            login: {
                email: '',
                password: ''
            },
        }   
    },
    methods:{
        onLogin(e) {
            AV.User.logIn(this.login.email, this.login.password).then((user) => {
                user = user.toJSON()
                this.$emit('login',user)
            }, (error) => {
                console.log(error)
                if (error.code === 211) {
                    alert('邮箱未注册')
                } else if (error.code === 210) {
                    alert('邮箱或密码错误')
                }
            });
        },
        onClickSignUp(){
            this.$emit('goTosignUp')
        },
        onCloseLogin() {
            this.$emit('close')
        },
    },
    template:`
    <div class="login" v-cloak>
        <form class="form" @submit.prevent="onLogin">
            <h2>登入</h2>
            <button type="button" @click="onCloseLogin">关闭</button>
            <div class="row">
                <label>邮箱</label>
                <input type="text" v-model="login.email">
            </div>
            <div class="row">
                <label>密码</label>
                <input type="password" v-model="login.password">
            </div>
            <div class="actions">
                <button type="submit">提交</button>
                <a href="#" @click="onClickSignUp">注册</a>
            </div>
        </form>
    </div>
    `
})